package com.cyc.basic.controller;

import com.cyc.basic.pojo.entiy.SysUser;
import com.cyc.basic.pojo.vo.UserReq;
import com.cyc.basic.service.user.ISysUserService;
import com.cyc.basic.test.other.AsyncTask;
import com.cyc.basic.util.JobAddressUtil;
import com.cyc.common.annotation.PathRestControllerAnn;
import com.cyc.common.exception.ServiceException;
import com.cyc.common.model.ApiResult;
import com.cyc.common.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;

/**
 * @author chenyunchang
 * @title
 * @date 2020/12/17 10:25
 * @Description:
 */
@Api(tags = "测试类")
@PathRestControllerAnn("/test")
public class TestController {

    @Autowired
    private AsyncTask asyncTask;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ISysUserService userService;

    @ApiOperation("测试1")
    @GetMapping("/test1")
    public String test1() {
        return "访问成功";
    }

    @ApiOperation("测试统一异常拦截")
    @GetMapping("/testInterceptorException")
    public String testInterceptorException(Integer sign) {
        if (Objects.equals(1, sign)) {
            throw new ServiceException("400", "测试异常拦截");
        }
        if (Objects.equals(2, sign)) {
            throw new ServiceException("400", "测试异常拦截");
        }
        return "访问成功";
    }

    @GetMapping("/testThread")
    public String testThread(Integer i) {
        asyncTask.tesTask(i);
        return "访问成功";
    }

    @ApiOperation("查询用户列表")
    @PostMapping("/findPage")
    public Result<SysUser> findPage(@Validated @RequestBody UserReq userReq) {
        return Result.success(userService.getById(1));
    }

    @ApiOperation("测试redis")
    @PostMapping("/redisTest")
    public String redisTest(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
        return "操作成功";
    }

    @ApiOperation("测试swagger枚举类型")
    @PostMapping("/swaggerEnumTest")
    public String swaggerEnumTest(UserReq userReq) {
        return "查询成功";
    }

    @ApiOperation("case when test")
    @PostMapping("/caseWhenTest")
    public String caseWhenTest(UserReq userReq) {
        userService.caseWhenTest(userReq);
        return "查询成功";
    }

    /**
     * @param userReq 用户请求参数
     * @return ApiResult<SysUser>
     * @author chenyunchang
     * @description 日期类型查询
     * @date 2021-04-07 14:28:10
     */
    @ApiOperation("日期类型查询")
    @PostMapping("/dateTest")
    public ApiResult<List<SysUser>> dateTest(@RequestBody UserReq userReq) {
        List<SysUser> userList = userService.dateTest(userReq);
        return ApiResult.success(userList);
    }

    @ApiOperation("mybatisPlus的批量插入问题")
    @PostMapping("/batchInsertTest")
    public ApiResult<SysUser> batchInsertTest() {
        userService.batchInsertTest();
        return ApiResult.success();
    }

    @ApiOperation("测试updateByIdTest")
    @PostMapping("/updateByIdTest")
    public ApiResult<SysUser> updateByIdTest() {
        userService.updateByIdTest();
        return ApiResult.success();
    }

    @ApiOperation("替换城市")
    @PostMapping("/checkAddress")
    public String checkAddress(String address) {
        return JobAddressUtil.replacCityDq(address);
    }
}
